package cn.Alibaba.pet.service.impl;

import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.util.BaiduAuditUtils;
import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.pet.domain.Pet;
import cn.Alibaba.pet.domain.PetDetail;
import cn.Alibaba.pet.domain.Product;
import cn.Alibaba.pet.domain.ProductDetail;
import cn.Alibaba.pet.mapper.ProductDetailMapper;
import cn.Alibaba.pet.mapper.ProductMapper;
import cn.Alibaba.pet.service.IProductService;
import cn.Alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;


    @Override
    public void save(Product product) {



        Boolean aBoolean = BaiduAuditUtils.TextCensor(product.getName());
        if (aBoolean){
            ProductDetail productDetail = product.getProductDetail();
            if (productDetail!=null){
                productDetail.setProductId(product.getId());
                productDetailMapper.add(productDetail);
            }
        }else {
            throw  new BusinessException("审核不通过");
        }
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.add(productDetail);

    }

    @Override
    public void update(Product product) {



        Boolean aBoolean = BaiduAuditUtils.TextCensor(product.getName());
        if (aBoolean){
            ProductDetail productDetail = product.getProductDetail();
            if (productDetail!=null){
                productDetail.setProductId(product.getId());
                productDetailMapper.updateproductDetailById(productDetail);
                super.update(product);
            }

        }else {
            throw new BusinessException("审核不通过");
        }



    }

    @Override
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delproductDetailById(id);

    }


    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {

        for (Long id : ids) {

            Product product = productMapper.loadOne(id);

            Boolean textBoo = BaiduAuditUtils.TextCensor(product.getName());
            Boolean imgBoo = BaiduAuditUtils.ImgCensor(product.getResources());
            if (textBoo ){
                product.setOnsaletime(new Date());
                product.setState(1l);
                productMapper.update(product);
            }else {
              throw new BusinessException("审核失败");

            }
        }
    }

    @Override
    public void offsale(List<Long> ids) {
        Date offsaletime = new Date();

        productMapper.offsale(offsaletime,ids);

    }
}
